﻿using System.Collections.Generic;
using TwitterBootstrapMVC;
using Web.UI.AdminLTE.Common;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Controls.Tab
{
    public class TabHeader : HtmlElementBase
    {
        private string _appendBadgeText;
        private readonly bool _isActive;
        private readonly string _labelText;
        private readonly string _href;
        private Icon _prependIcon;
        private Icon _appendIcon;
        private bool _isDisabled;

        public TabHeader(string labelText, string href, bool isActive)
        {
            _labelText = labelText;
            _href = href;
            _isActive = isActive;
        }

        public override string ToHtmlString()
        {
            var iconPrepend = _prependIcon != null ? string.Format("{0} ", _prependIcon) : string.Empty;
            var iconAppend = _appendIcon != null ? string.Format(" {0}", _appendIcon) : string.Empty;
            var badge = !string.IsNullOrEmpty(_appendBadgeText)
                ? string.Format(" <span class='badge'>{0}</span>", _appendBadgeText)
                : string.Empty;
            return TagBuilderExtensions.New("li")
                     .AddOrMergeCssClass(_isDisabled, "disabled")
                     .AddOrMergeCssClass(_isActive, "active")
                     .Html(TagBuilderExtensions.New("a")
                         .AddOrMergeAttribute("data-toggle", "tab")
                         .AddOrMergeAttribute("href", "#" + _href)
                         .MergeHtmlAttributes(HtmlAttr.FormatHtmlAttributes())
                         .Html(string.Format("{0}{1}{2}{3}", iconPrepend, _labelText, iconAppend, badge)))
                     .ToString();
        }

        public TabHeader PrependIcon(Icon icon)
        {
            _prependIcon = icon;
            return this;
        }

        public TabHeader AppendIcon(Icon icon)
        {
            _appendIcon = icon;
            return this;
        }

        public TabHeader AppendBadge(string badgeText)
        {
            _appendBadgeText = badgeText;
            return this;
        }

        public TabHeader Disabled(bool isDisabled = true)
        {
            _isDisabled = isDisabled;
            return this;
        }

        public TabHeader Id(string id)
        {
            SetId(id);
            return this;
        }

        public TabHeader HtmlAttributes(IDictionary<string, object> htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public TabHeader HtmlAttributes(object htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes.ToDictionary());
            return this;
        }

        public TabHeader Class(string cssClass)
        {
            EnsureClass(cssClass);
            return this;
        }

        public TabHeader Data(object htmlDataAttributes)
        {
            MergeHtmlAttributes(htmlDataAttributes.ToHtmlDataAttributes());
            return this;
        }
    }
}